package com.mall.service.home;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.home.MallBannerService;
import com.mall.custom.dao.read.MallBannerReadMapper;
import com.mall.custom.dao.write.MallBannerWriteMapper;
import com.mall.pojo.MallBanner;
import com.mall.pojo.MallBannerExample;
import com.mall.pojo.MallBannerExample.Criteria;
import com.mall.query.MallBannerQuery;
import com.mall.query.Page;

@Service
public class MallBannerServiceImpl implements MallBannerService {
	
	@Autowired
	private MallBannerReadMapper mallBannerReadMapper;
	@Autowired
	private MallBannerWriteMapper mallBannerWriteMapper;

	@Override
	public List<MallBanner> getMallBannerByMallQuery(MallBannerQuery mallQuery) {

		if (mallQuery == null || mallQuery.getOnlyKey() == null || mallQuery.getOnlyKey() <= 0 || mallQuery.getType() == null
				|| mallQuery.getType() < 0) {
			return null;
		}

		MallBannerExample example = new MallBannerExample();
		Page page = mallQuery.getPage();
		if (page == null) {
			page = Page.getInitPage();
		}
		if (StringUtils.isNotBlank(mallQuery.getOrderBy())) {
			example.setOrderByClause(mallQuery.getOrderBy());
		}
		Criteria criteria = example.createCriteria();
		if (mallQuery.getOnlyKey() != null) {
			criteria.andCommOnlyKeyEqualTo(mallQuery.getOnlyKey());
		}
		if (mallQuery.getType() != null) {
			criteria.andTypeEqualTo(mallQuery.getType());
		}
		if (mallQuery.getStatus() != null) {
			criteria.andStatusEqualTo(mallQuery.getStatus());
		}

		return mallBannerReadMapper.selectByExample(example);
	}

	@Override
	public Integer insertMallBanner(MallBanner mallBanner) {
		mallBanner.setGmtCreate(new Date());
		mallBanner.setGmtModify(new Date());
		return this.mallBannerWriteMapper.insertSelective(mallBanner);
	}

}
